﻿namespace Vdc.Grid
{
    using Vdc.Interface;
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    internal class GridFilter : WebControl, INamingContainer
    {
        private int columnIndex;
        private Vdc.Grid.Grid grid;
        private HtmlContainer innerFilter;

        public GridFilter(Vdc.Grid.Grid _grid)
        {
            this.grid = _grid;
            this.ID = this.GetFilterID();
        }

        protected virtual GridDataControlFieldCell CreateCell(Column field)
        {
            GridDataControlFieldCell cell = new GridDataControlFieldCell(field);
            if (field.Visible)
            {
                cell.CssClass = !field.Wrap ? "ob_gC" : "ob_gCW";
            }
            else
            {
                cell.CssClass = !field.Wrap ? "ob_gC_I" : "ob_gCW_I";
            }
            if (field.Index == this.grid.IndexOfFirstVisibleColumn)
            {
                cell.CssClass = cell.CssClass + " ob_gC_Fc";
            }
            else if (field.Index == this.grid.IndexOfLastVisibleColumn)
            {
                cell.CssClass = cell.CssClass + " ob_gC_Lc";
            }
            if (field.Width.IndexOf("%") == -1)
            {
                cell.Width = Unit.Pixel(int.Parse(field.Width));
            }
            else
            {
                cell.Width = Unit.Percentage((double) int.Parse(field.Width.Replace("%", "")));
            }
            if (this.grid.IsDesignTimeRender)
            {
                if (!field.Visible)
                {
                    cell.Style["display"] = "none";
                    cell.Width = Unit.Pixel(0);
                }
                cell.Attributes["_designerRegion"] = field.Index.ToString();
            }
            HtmlContainer child = new HtmlContainer {
                CssClass = "ob_gCc1"
            };
            cell.Controls.Add(child);
            if ((!string.IsNullOrEmpty(field.DataField) && field.AllowFilter) && this.grid.internalDataBinding)
            {
                HtmlContainer container2 = new HtmlContainer();
                HtmlContainer container3 = new HtmlContainer();
                HtmlContainer container4 = new HtmlContainer();
                container2.CssClass = "ob_gCc2";
                container3.CssClass = "ob_gCc2";
                if (field.ShowFilterCriterias)
                {
                    VdcDropDownList list = new VdcDropDownList
                    {
                        ID = "ob_" + this.ID + "FCTb" + this.columnIndex.ToString(),
                        Width = Unit.Percentage(100.0),
                        MenuWidth = Unit.Pixel(200),
                        FolderStyle = this.grid.GetInterfaceFolderStyle_DropDownList()
                    };
                    if (field.HasCustomFilterOptionsWithTemplates())
                    {
                        list.ClientSideEvents.OnSelectedIndexChanged = string.Concat(new object[] { "function() {try{return ", this.grid.GridID, ".onFODdlChange(", field.Index, ");}catch(ex){}}" });
                    }
                    field.FilterOptionsDdlID = list.ID;
                    List<FilterOption> filterOptions = field.GetFilterOptions();
                    int num = 0;
                    foreach (FilterOption option in filterOptions)
                    {
                        list.Items.Add(new ListItem(option.GetText(this.grid), option.GetID()));
                        if (option.Type == field.FilterCriteria.Option.Type)
                        {
                            list.SelectedIndex = num;
                        }
                        num++;
                    }
                    container2.Controls.Add(list);
                    child.Controls.Add(container2);
                }
                else
                {
                    container2.Visible = false;
                    VdcDropDownList list3 = new VdcDropDownList();
                    container2.Controls.Add(list3);
                    child.Controls.Add(container2);
                }
                child.Controls.Add(container3);
                container3.Controls.Add(container4);
                if (string.IsNullOrEmpty(field.TemplateSettings.FilterTemplateId))
                {
                    VdcTextBox box = new VdcTextBox
                    {
                        ID = "ob_" + this.grid.GridID + "FilterControl" + this.columnIndex.ToString()
                    };
                    box.Attributes["autocomplete"] = "off";
                    box.FolderStyle = this.grid.GetInterfaceFolderStyle_TextBox();
                    box.Width = Unit.Percentage(100.0);
                    box.Text = field.FilterCriteria.Value;
                    box.ClientSideEvents.OnKeyDown = "function(s,k) {try{return " + this.grid.GridID + ".onFTBKeyDown(s,k);}catch(ex){}}";
                    box.ClientSideEvents.OnKeyPress = "function(s,k) {try{return " + this.grid.GridID + ".onFTBKeyPress(s,k);}catch(ex){}}";
                    container4.Controls.Add(box);
                    field.FilterControlID = box.ID;
                    field.FilterControlPropertyName = "value";
                    return cell;
                }
                GridTemplate byId = null;
                GridRuntimeTemplate runtimeTemplateById = null;
                if (this.grid.Templates.GetById(field.TemplateSettings.FilterTemplateId) is GridRuntimeTemplate)
                {
                    runtimeTemplateById = this.grid.Templates.GetRuntimeTemplateById(field.TemplateSettings.FilterTemplateId);
                    if (runtimeTemplateById == null)
                    {
                        throw new InvalidTemplateIDException(field.TemplateSettings.FilterTemplateId);
                    }
                }
                else
                {
                    byId = this.grid.Templates.GetById(field.TemplateSettings.FilterTemplateId);
                    if (byId == null)
                    {
                        throw new InvalidTemplateIDException(field.TemplateSettings.FilterTemplateId);
                    }
                }
                if (byId != null)
                {
                    container4.Controls.Add(byId);
                    this.grid.StoreFilterTemplatesReferences(container4, byId, field.Index);
                }
                if (runtimeTemplateById != null)
                {
                    container4.Controls.Add(runtimeTemplateById);
                    this.grid.StoreFilterTemplatesReferences(container4, runtimeTemplateById, field.Index);
                }
                return cell;
            }
            child.Text = "&#160;";
            field.FilterControlID = "";
            field.FilterControlPropertyName = "";
            field.FilterOptionsDdlID = "";
            return cell;
        }

        protected virtual void CreateCellSeparator(string width, bool visible)
        {
            HtmlContainer child = new HtmlContainer {
                Text = "&#160;",
                CssClass = "ob_gCS"
            };
            child.Style["left"] = width;
            if (!visible || this.grid.HasColumnsWithFixedAndPercentageWidth)
            {
                child.Style["display"] = "none";
            }
            this.innerFilter.Controls.Add(child);
        }

        protected override void CreateChildControls()
        {
            this.CssClass = "ob_gFlCont";
            string str = "";
            if ((this.grid.FilterType != GridFilterType.ProgrammaticOnly) && !this.grid.IsDesignTimeRender)
            {
                if ((this.grid.FilteringSettings.InitialState == GridFilterState.Hidden) && ((string.IsNullOrEmpty(this.grid.FilterExpression) && (string.IsNullOrEmpty(this.grid.FilteringButtonsConfigurationValue) || (this.grid.FilteringButtonsConfigurationValue.IndexOf("3") == -1))) || (!this.grid.CallbackMode && (this.grid.FilteringButtonsConfigurationValue == "1"))))
                {
                    str = "none";
                }
            }
            else
            {
                str = "none";
            }
            base.Style["display"] = str;
            if (!this.grid.CallbackMode || !this.grid.IsCallback)
            {
                this.innerFilter = new HtmlContainer();
                this.innerFilter.CssClass = "ob_gFlICont";
                Table child = new Table {
                    CssClass = "ob_gFl"
                };
                child.Attributes["cellspacing"] = "0";
                child.Attributes["cellpadding"] = "0";
                child.Width = Unit.Percentage(100.0);
                this.innerFilter.Controls.Add(child);
                this.Controls.Add(this.innerFilter);
                GridRow row = new GridRow(0, 0, GridRowType.Filter, DataControlRowState.Normal);
                child.Controls.Add(row);
                int num = 0;
                int num2 = 0;
                foreach (Column column in this.grid.Columns)
                {
                    this.columnIndex = num;
                    row.Controls.Add(this.CreateCell(column));
                    if (num < (this.grid.Columns.Count - 1))
                    {
                        if (column.Width.IndexOf("%") == -1)
                        {
                            this.CreateCellSeparator(((num2 + int.Parse(column.Width))).ToString() + "px", column.Visible);
                        }
                        else
                        {
                            this.CreateCellSeparator(((num2 + int.Parse(column.Width.Replace("%", "")))).ToString() + "%", column.Visible);
                        }
                    }
                    if (column.Visible)
                    {
                        num2 += int.Parse(column.Width.Replace("%", ""));
                    }
                    num++;
                }
                GridRowEventArgs e = new GridRowEventArgs(row);
                this.grid.OnRowCreated(e);
            }
            base.CreateChildControls();
        }

        internal void ForceEnsureChildControls()
        {
            this.EnsureChildControls();
        }

        protected virtual string GetFilterID()
        {
            return ("ob_" + this.grid.GridID + "FilterContainer");
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }
    }
}

